<?php
class main extends base{

	public function init(){
		parent::init();

		$this->tit = '首页';

		$this->companyObj = D('company');
        $this->orderObj = D('company_order');
        $this->zoneObj = D('company_zone');

        $this->fankuiObj = D('company_fankui');
	}

	public function index()
	{
        // 最近七日
        // $s_time = date('Y-m-d 00:00:00', strtotime('-7 days'));
        // $e_time = date("Y-m-d 00:00:00");
        // dump($s_time);
        // dump($e_time);
        // exit;

        // 今日
        $s_time = date('Y-m-d 00:00:00');
        // $e_time = date('Y-m-d 00:00:00');
        $cond = "status=1 and order_status<99 ".$this->cond_base;
        $cond .= " and add_time>'{$s_time}'";
		$count1 = $this->fankuiObj->findCount(array($cond));

        // 总量
		$cond = "status=1 and order_status<99 ".$this->cond_base;
		$count2 = $this->fankuiObj->findCount(array($cond));

        // 已完成
        $cond = "status=1 and order_status>=5 and order_status<99 ".$this->cond_base;
		$count3 = $this->fankuiObj->findCount(array($cond));

        // 未完成
        $cond = "status=1 and order_status<5 and order_status>1 ".$this->cond_base;
		$count4 = $this->fankuiObj->findCount(array($cond));


        // 科室排行
        $zones = $this->zoneObj->findAll(array('status=1'.$this->cond_base));
        if($zones){
            foreach($zones as $val){
                $v[$val['id']] = $this->getOrderTotalByZone($val['id']);
                $v[$val['id']]['name'] = $val['name'];
            }
        }

        // 最近七日全部科室
        $lately7days = $this->getLately7days_zone();
        $zone_ids = $this->getLately7days_zone_id();
        // dump($zone_ids);
        $order_total = $this->getOrderTotalByDate_zone($zone_ids);
        $order_total_price = $this->getOrderTotalByDate2_zone($zone_ids);

        // 最近七天日期
        $latelycate = ['订单量', '完成量'];
        // $lately7days = $this->getLately7days();
        // $order_total = $this->getOrderTotalByDate($lately7days);
        // $order_total_price = $this->getOrderTotalByDate2($lately7days);

		$data = [
            'widget-card' => [
                'count1' => $count1,
                'count2' => $count2,
                'count3' => $count3,
                'count4' => $count4,
            ],
            // 'widget-outline' => [
            //     'order_total_price' => [
            //         'tday' => 0,
            //         'ytd' => 0
            //     ],
            //     'order_total' => [
            //         'tday' => 0,
            //         'ytd' => 0
            //     ],
            //     // 新增用户数
            //     'new_user_total' => [
            //         'tday' => 0,
            //         'ytd' => 0
            //     ],
            //     // 下单用户数
            //     'order_user_total' => [
            //         'tday' => 0,
            //         'ytd' => 0
            //     ]
            // ],
            'widget-echarts' => [
                // 最近七天日期
                'cate' => json_encode($latelycate,JSON_UNESCAPED_UNICODE),
                'date' => json_encode($lately7days,JSON_UNESCAPED_UNICODE),
                'order_total' => json_encode($order_total,true),
                'order_total_price' => json_encode($order_total_price,true),
            ]
		];
        // dump($data);exit;
		$this->data = $data;
        // dump($this->data);exit;
	}



    private function getLately7days()
    {
        // 获取当前周几
        $date = [];
        for ($i = 0; $i < 7; $i++) {
            $date[] = date('Y-m-d', strtotime('-' . $i . ' days'));
        }
        return array_reverse($date);
    }

    private function getOrderTotalByDate($days)
    {
        $data = [];
        foreach ($days as $day) {
            $data[] = $this->getOrderTotal($day);
        }
        return $data;
    }

    private function getOrderTotal($day = null)
    {
        // 今日
        $s_time = date("{$day} 00:00:00");
        $e_time = date("{$day} 23:59:59");
        $cond = "status=1 and order_status<99 ".$this->cond_base;
        $cond .= " and add_time>='{$s_time}' and add_time<='{$e_time}'";
		$count1 = $this->fankuiObj->findCount(array($cond));

        return number_format($count1);
    }

    private function getOrderTotalByDate2($days)
    {
        $data = [];
        foreach ($days as $day) {
            $data[] = $this->getOrderTotal2($day);
        }
        return $data;
    }

    private function getOrderTotal2($day = null)
    {
        // 今日
        $s_time = date("{$day} 00:00:00");
        $e_time = date("{$day} 23:59:59");
        $cond = "status=1 and order_status>=5 and order_status<99 ".$this->cond_base;
        $cond .= " and add_time>='{$s_time}' and add_time<='{$e_time}'";
		$count1 = $this->fankuiObj->findCount(array($cond));

        return number_format($count1);
    }



    // 科室统计
    private function getOrderTotalByZone($zone_id)
    {
        // 总量
		$cond = "status=1 and order_status<99 ".$this->cond_base;
        $cond .= " and zone_id='{$zone_id}'";
		$count2 = $this->fankuiObj->findCount(array($cond));

        // 已完成
        $cond = "status=1 and order_status>=5 and order_status<99 ".$this->cond_base;
        $cond .= " and zone_id='{$zone_id}'";
		$count3 = $this->fankuiObj->findCount(array($cond));

        // 未完成
        $cond = "status=1 and order_status<5 and order_status>1 ".$this->cond_base;
        $cond .= " and zone_id='{$zone_id}'";
		$count4 = $this->fankuiObj->findCount(array($cond));

        $data['count2'] = number_format($count2);
        $data['count3'] = number_format($count3);
        $data['count4'] = number_format($count4);


        return $data;
    }

    // 全部科室名称
    private function getLately7days_zone($num=50)
    {
        // 获取当前科室
        $date = [];
        $zones = $this->zoneObj->findAll(array('status=1 and parent_id>0 '.$this->cond_base),'sort asc,id asc','*',$num);
        if($zones){
            foreach($zones as $val){
                $date[] = $val['name'];
            }
        }
        // $date = [];
        // for ($i = 0; $i < 7; $i++) {
        //     $date[] = date('Y-m-d', strtotime('-' . $i . ' days'));
        // }
        return array_reverse($date);
    }
    // 全部科室id
    private function getLately7days_zone_id()
    {
        // 获取当前科室
        $date = [];
        $zones = $this->zoneObj->findAll(array('status=1 and parent_id>0 '.$this->cond_base));
        if($zones){
            foreach($zones as $val){
                $date[] = $val['id'];
            }
        }
        // $date = [];
        // for ($i = 0; $i < 7; $i++) {
        //     $date[] = date('Y-m-d', strtotime('-' . $i . ' days'));
        // }
        return array_reverse($date);
    }

    // 当前科室统计
    private function getOrderTotalByDate_zone($zone_ids)
    {
        $data = [];
        foreach ($zone_ids as $val) {
            $data[] = $this->getOrderTotal_zone($val);
        }
        return $data;
    }

    // 当前科室最近7日统计
    private function getOrderTotal_zone($zone_id=0)
    {
        // 最近七日
        $s_time = date('Y-m-d 00:00:00', strtotime('-7 days'));
        $e_time = date("Y-m-d 00:00:00");
        // 今日
        // $s_time = date("{$day} 00:00:00");
        // $e_time = date("{$day} 23:59:59");
        $cond = "status=1 and order_status<99 ".$this->cond_base;
        $cond .= " and zone_id='{$zone_id}'";
        $cond .= " and add_time>='{$s_time}' and add_time<='{$e_time}'";
		$count1 = $this->fankuiObj->findCount(array($cond));

        return number_format($count1);
    }


    private function getOrderTotalByDate2_zone($zone_ids)
    {
        $data = [];
        foreach ($zone_ids as $val) {
            $data[] = $this->getOrderTotal2_zone($val);
        }
        return $data;
    }

    private function getOrderTotal2_zone($zone_id=0)
    {
        // 最近七日
        $s_time = date('Y-m-d 00:00:00', strtotime('-7 days'));
        $e_time = date("Y-m-d 00:00:00");
        // 今日
        // $s_time = date("{$day} 00:00:00");
        // $e_time = date("{$day} 23:59:59");
        $cond = "status=1 and order_status>=5 and order_status<99 ".$this->cond_base;
        $cond .= " and zone_id='{$zone_id}'";
        $cond .= " and add_time>='{$s_time}' and add_time<='{$e_time}'";
		$count1 = $this->fankuiObj->findCount(array($cond));

        return number_format($count1);
    }
}